package cn.iocoder.yudao.module.train.controller.admin.viplevel;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.viplevel.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.viplevel.VipLevelDO;
import cn.iocoder.yudao.module.train.service.viplevel.VipLevelService;

@Tag(name = "管理后台 - 会员等级设定")
@RestController
@RequestMapping("/train/vip-level")
@Validated
public class VipLevelController {

    @Resource
    private VipLevelService vipLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建会员等级设定")
    @PreAuthorize("@ss.hasPermission('train:vip-level:create')")
    public CommonResult<Integer> createVipLevel(@Valid @RequestBody VipLevelSaveReqVO createReqVO) {
        return success(vipLevelService.createVipLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级设定")
    @PreAuthorize("@ss.hasPermission('train:vip-level:update')")
    public CommonResult<Boolean> updateVipLevel(@Valid @RequestBody VipLevelSaveReqVO updateReqVO) {
        vipLevelService.updateVipLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级设定")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:vip-level:delete')")
    public CommonResult<Boolean> deleteVipLevel(@RequestParam("id") Integer id) {
        vipLevelService.deleteVipLevel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除会员等级设定")
                @PreAuthorize("@ss.hasPermission('train:vip-level:delete')")
    public CommonResult<Boolean> deleteVipLevelList(@RequestParam("ids") List<Integer> ids) {
        vipLevelService.deleteVipLevelListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员等级设定")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:vip-level:query')")
    public CommonResult<VipLevelRespVO> getVipLevel(@RequestParam("id") Integer id) {
        VipLevelDO vipLevel = vipLevelService.getVipLevel(id);
        return success(BeanUtils.toBean(vipLevel, VipLevelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员等级设定分页")
    @PreAuthorize("@ss.hasPermission('train:vip-level:query')")
    public CommonResult<PageResult<VipLevelRespVO>> getVipLevelPage(@Valid VipLevelPageReqVO pageReqVO) {
        PageResult<VipLevelDO> pageResult = vipLevelService.getVipLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VipLevelRespVO.class));
    }

    @GetMapping("/allLevel")
    @Operation(summary = "获得全部会员等级")
    @PreAuthorize("@ss.hasPermission('train:vip-level:query')")
    public CommonResult<List<VipLevelReqVO>> getAllVipLevelPage() {
        return CommonResult.success(BeanUtils.toBean(vipLevelService.getAllVipLevel(), VipLevelReqVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员等级设定 Excel")
    @PreAuthorize("@ss.hasPermission('train:vip-level:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVipLevelExcel(@Valid VipLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VipLevelDO> list = vipLevelService.getVipLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员等级设定.xls", "数据", VipLevelRespVO.class,
                        BeanUtils.toBean(list, VipLevelRespVO.class));
    }

}
